//
//  LastView.m
//  OpenEyes
//
//  Created by dllo on 15/9/15.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LastView.h"
#import "LastModel.h"

@implementation LastView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
       
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:self.frame];
        self.scrollView.contentSize = CGSizeMake(0, 700);
        self.scrollView.backgroundColor = [UIColor colorWithWhite:0.220 alpha:1.000];
        [self addSubview:self.scrollView];
        
        
        self.img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, H * 400)];
        self.img.userInteractionEnabled = YES;
        [self.scrollView addSubview:self.img];
        
        //在动画效果图添加一层透明视图 目的点击
        self.clearView =[[ UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, H * 400)];
        self.clearView.backgroundColor = [UIColor clearColor];
        [self.scrollView addSubview:self.clearView];
        
        
        //挡住放大图片的剩余
        UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, WIDTH/375*400 , WIDTH/375* WIDTH ,  HEIGHT/667* 300)];
        [backView setBackgroundColor:[UIColor colorWithRed:0.220 green:0.220 blue:0.221 alpha:1.000]];
        [self.scrollView addSubview:backView];
        
        
        self.titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 375 * 20, H * 410, WIDTH - 40, H * 30)];
        self.titleLbl.font = [UIFont boldSystemFontOfSize:18];
        self.titleLbl.textColor = [UIColor whiteColor];
        [self.scrollView addSubview:self.titleLbl];
        
        
        self.contentLbl = [[UILabel alloc]initWithFrame:CGRectMake(W * 20, H * 470, WIDTH - 40, 30)];
        self.contentLbl.font = [UIFont boldSystemFontOfSize:13];
        self.contentLbl.textColor = [UIColor whiteColor];
        [self.scrollView addSubview:self.contentLbl];
        
        
        
        self.fadeInView = [[LazyFadeInView alloc] init];
        [self.scrollView addSubview:self.fadeInView];
        
        
        self.priceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.priceBtn.frame = CGRectMake(WIDTH / 375 * 20, HEIGHT / 667 * 510, WIDTH / 375 * 80, HEIGHT / 667 * 30);
        self.priceBtn.backgroundColor = [UIColor whiteColor];
        [self.priceBtn setTitleColor:[UIColor colorWithRed:1.000 green:0.369 blue:0.361 alpha:1.000] forState:UIControlStateNormal];
        [self.priceBtn.layer setMasksToBounds:YES];
        [self.priceBtn.layer setCornerRadius:2];
        [self.priceBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [self.priceBtn addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:self.priceBtn];
        
        
        
        self.lookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.lookBtn.frame = CGRectMake(WIDTH / 375 * 100, HEIGHT / 667 * 510, WIDTH / 375 * 80, HEIGHT / 667 * 30);
        self.lookBtn.backgroundColor = [UIColor colorWithRed:1.000 green:0.369 blue:0.360 alpha:1.000];
        [self.lookBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.lookBtn.layer setMasksToBounds:YES];
        [self.lookBtn.layer setCornerRadius:2];
        [self.lookBtn setTitle:@"去看看" forState:UIControlStateNormal];
        [self.lookBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [self.lookBtn addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:self.lookBtn];
        
        
        
        /**
         *  更多图片
         */
        self.morePicLbl = [[UILabel alloc]initWithFrame:CGRectMake(W * 325, H * 340, W * 50, H * 40)];
        self.morePicLbl.backgroundColor = [UIColor blackColor];
        [self.scrollView addSubview:self.morePicLbl];
        
        self.morePicImg = [[UIImageView alloc]initWithFrame:CGRectMake(W * 335, H * 345, W * 30,  H * 30)];
        self.morePicImg.image = [UIImage imageNamed:@"图片详情"];
        [self.scrollView addSubview:self.morePicImg];
        
       
        
        self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
        [self.clearView addGestureRecognizer:self.tap];
        
        [self createButtonWithAddAndShare];
    }
    
    return self;
}

-(void)createButtonWithAddAndShare
{
    
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.addButton setImage:[UIImage imageNamed:@"iconfont-Collection"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"iconfont-shoucang1"] forState:UIControlStateSelected];
    [self.scrollView addSubview:self.addButton];
    
    
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareButton addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.shareButton setImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    [self.shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    [self.scrollView addSubview:self.shareButton];
    
}

-(void)addClick:(UIButton *)sender
{
    
    sender.selected = !sender.selected;
    
}

-(void)shareClick:(UIButton *)sender
{
    
    
}

-(void)getValue:(LastModel *)model
{
    [self.img sd_setImageWithURL:[NSURL URLWithString:model.coverImage] placeholderImage:nil];
    self.titleLbl.text = model.title;
    
    CGFloat contentH = [HeightWithString heightForTextLabel:model.content width:WIDTH - 40 fontSize:13];
//    self.contentLbl.numberOfLines = 0;
    self.fadeInView.frame = CGRectMake(W * 20, H * 450, WIDTH - 40, contentH+20);
    self.fadeInView.text = model.content;
    
    
    self.priceBtn.frame = CGRectMake(W * 20, contentH + H * 490, W * 80, H * 30);
    UIView * priceView = [[UIView alloc] initWithFrame:CGRectMake(W * 99, contentH + H * 490, W * 1, H * 30)];
    priceView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:priceView];
    
    
    self.lookBtn.frame = CGRectMake(W * 100, contentH + H * 490, W * 80, H * 30);
    UIView * lookView = [[UIView alloc] initWithFrame:CGRectMake( W * 100, contentH + H * 490, W * 1, H * 30)];
    lookView.backgroundColor = [UIColor colorWithRed:1.000 green:0.369 blue:0.360 alpha:1.000];
    [self.scrollView addSubview:lookView];
    
    CGFloat priceStr = [model.promotionPrice floatValue];
    [self.priceBtn setTitle:[NSString stringWithFormat:@"￥%.1f",priceStr] forState:UIControlStateNormal];
    
    self.scrollView.contentSize = CGSizeMake(0, contentH + H * 520);
   
//    全局   传值
    self.webViewUrl = model.promotionUrl;
    self.picImage = model.image;
    
    [self.addButton setFrame:CGRectMake(240, contentH + H * 490, W * 30, H * 30)];
    
    [self.shareButton setFrame:CGRectMake(300,contentH + H * 493, W * 27, H * 27)];


}


-(void)buy
{
   
    BuyVC *buy = [[BuyVC alloc]init];
    [self.delegate push:buy];
    buy.getBuyUrl = self.webViewUrl;

}


-(void)tapClick
{
    if (![self.picImage isEqualToString:@""]) {
        MorePicVC *morePic = [[MorePicVC alloc]init];
        morePic.getPicImage = self.picImage;
        [self.morePicDelegate pushTo:morePic];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
